package com.mdd.admin.service.reply.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.reply.IOfficialAccountReplyService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.reply.OfficialAccountReplyParam;
import com.mdd.admin.vo.reply.OfficialAccountReplyListVo;
import com.mdd.admin.vo.reply.OfficialAccountReplyDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.reply.OfficialAccountReply;
import com.mdd.common.mapper.reply.OfficialAccountReplyMapper;
import com.mdd.common.utils.ArrayUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import com.mdd.common.config.GlobalConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 微信公众号自动回复实现类
 */
@Service
public class OfficialAccountReplyServiceImpl implements IOfficialAccountReplyService {
        
    @Resource
    OfficialAccountReplyMapper officialAccountReplyMapper;

    /**
     * 微信公众号自动回复列表
     *
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return PageResult<OfficialAccountReplyListVo>
     */
    @Override
    public PageResult<OfficialAccountReplyListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page  = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        QueryWrapper<OfficialAccountReply> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(Arrays.asList("sort", "id"));

        officialAccountReplyMapper.setSearch(queryWrapper, params, new String[]{
            "like:name:str",
            "=:keyword:str",
            "=:replyType@reply_type:int",
            "=:matchingType@matching_type:int",
            "=:contentType@content_type:int",
            "=:content:str",
            "=:status:int",
            "=:sort:long",
        });

        IPage<OfficialAccountReply> iPage = officialAccountReplyMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<OfficialAccountReplyListVo> list = new LinkedList<>();
        for(OfficialAccountReply item : iPage.getRecords()) {
            OfficialAccountReplyListVo vo = new OfficialAccountReplyListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 微信公众号自动回复详情
     *
     * @param id 主键参数
     * @return OfficialAccountReply
     */
    @Override
    public OfficialAccountReplyDetailVo detail(Integer id) {
        OfficialAccountReply model = officialAccountReplyMapper.selectOne(
                new QueryWrapper<OfficialAccountReply>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        OfficialAccountReplyDetailVo vo = new OfficialAccountReplyDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 微信公众号自动回复新增
     *
     * @param officialAccountReplyParam 参数
     */
    @Override
    public void add(OfficialAccountReplyParam officialAccountReplyParam) {
        OfficialAccountReply model = new OfficialAccountReply();
        model.setName(officialAccountReplyParam.getName());
        model.setKeyword(officialAccountReplyParam.getKeyword());
        model.setReplyType(officialAccountReplyParam.getReplyType());
        model.setMatchingType(officialAccountReplyParam.getMatchingType());
        model.setContentType(officialAccountReplyParam.getContentType());
        model.setContent(officialAccountReplyParam.getContent());
        model.setStatus(officialAccountReplyParam.getStatus());
        model.setSort(officialAccountReplyParam.getSort());
        officialAccountReplyMapper.insert(model);
    }

    /**
     * 微信公众号自动回复编辑
     *
     * @param officialAccountReplyParam 参数
     */
    @Override
    public void edit(OfficialAccountReplyParam officialAccountReplyParam) {
        OfficialAccountReply model = officialAccountReplyMapper.selectOne(
                new QueryWrapper<OfficialAccountReply>()
                    .eq("id",  officialAccountReplyParam.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(officialAccountReplyParam.getId());
        model.setName(officialAccountReplyParam.getName());
        model.setKeyword(officialAccountReplyParam.getKeyword());
        model.setReplyType(officialAccountReplyParam.getReplyType());
        model.setMatchingType(officialAccountReplyParam.getMatchingType());
        model.setContentType(officialAccountReplyParam.getContentType());
        model.setContent(officialAccountReplyParam.getContent());
        model.setStatus(officialAccountReplyParam.getStatus());
        model.setSort(officialAccountReplyParam.getSort());
        officialAccountReplyMapper.updateById(model);
    }

    /**
     * 微信公众号自动回复删除
     *
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        OfficialAccountReply model = officialAccountReplyMapper.selectOne(
                new QueryWrapper<OfficialAccountReply>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        officialAccountReplyMapper.delete(new QueryWrapper<OfficialAccountReply>().eq("id", id));
    }

}
